﻿using System;

namespace CoreEx.ServiceModel
{
    /// <summary>
    /// Extensds the <see cref="Uri"/> class.
    /// </summary>
    public static class UriExtensions
    {
        /// <summary>
        /// Checks to see if the provided <paramref name="uri"/> represents a valid endpoint address.
        /// </summary>
        /// <param name="uri">The <see cref="Uri"/> instance to check.</param>
        /// <returns><b>True</b> if the <see cref="Uri"/> represents an endpoint address, otherwise <b>false</b></returns>
        public static bool IsValidEndpointAddress(this Uri uri)
        {
            return uri.OriginalString.EndsWith(".svc");
        }

        /// <summary>
        /// Combines the <paramref name="baseAddress"/> and the <paramref name="path"/> to form a valid endpoint address.
        /// </summary>
        /// <param name="baseAddress">The base address of the service endpoint.</param>
        /// <param name="path">The path top be added to the base address.</param>
        /// <returns>A new <see cref="Uri"/> that represents a valid endpoint address.</returns>
        public static Uri Combine(this Uri baseAddress, string path)
        {
            return new Uri(string.Format("{0}{1}",EnsureTrailingPathSeparator(baseAddress.OriginalString),path));
        }

        private static string EnsureTrailingPathSeparator(string urlString)
        {
            if (!urlString.EndsWith("/"))
                urlString += "/";
            return urlString;
        }

    }
}
